package com.webcamtracker.move.tracking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DefaultTrackingEventDispacher
        implements TrackingEventDispacher, Runnable {
    private Map<TrackingEventType, List<TrackingEventListener>> eventListeners;
    final private List<TrackingEvent> events = new ArrayList<TrackingEvent>();
    private boolean isAsynchronic = false;

    public DefaultTrackingEventDispacher() {
        this.eventListeners = new HashMap<TrackingEventType, List<TrackingEventListener>>();
        if (isAsynchronic) {
            Thread dispatcher = new Thread(this);
            dispatcher.setDaemon(true);
            dispatcher.start();
        }
    }

    public void setAsynchronic(boolean asynchronic) {
        isAsynchronic = asynchronic;
    }

    public void addTrackingEventListener(TrackingEventListener eventListener, TrackingEventType... trackingEventTypes) {
        for (TrackingEventType trackingType : trackingEventTypes) {
            if (getListenersForType(trackingType) == null) {
                this.eventListeners.put(trackingType, new ArrayList<TrackingEventListener>());
            }

            getListenersForType(trackingType).add(eventListener);
        }
    }

    public void fireTrackingEvent(TrackingEvent event) {
        if (isAsynchronic) {
            synchronized (events) {
                events.add(event);
                events.notifyAll();
            }
        } else {
            notifyTrackingEvent(event);
        }
    }


    protected void notifyTrackingEvent(TrackingEvent event) {
        if (!event.getType().isAll()) {
            for (TrackingEventListener listener : getListenersForType(event.getType())) {
                listener.trackingEventDispatched(event);
            }
        }

        for (TrackingEventListener listener : getListenersForType(TrackingEventType.ALL)) {
            listener.trackingEventDispatched(event);
        }
    }

    protected List<TrackingEventListener> getListenersForType(TrackingEventType trackingType) {
        List<TrackingEventListener> listeners = this.eventListeners.get(trackingType);

        if (listeners == null) {
            final ArrayList<TrackingEventListener> list = new ArrayList<TrackingEventListener>();
            this.eventListeners.put(trackingType, list);
            return list;
        }

        return listeners;
    }

    public boolean removeTrackingEventListener(TrackingEventListener eventListener, TrackingEventType... trackingEventTypes) {
        boolean result = false;

        for (TrackingEventType trackingType : trackingEventTypes) {
            final List<TrackingEventListener> list = getListenersForType(trackingType);
            result |= list.remove(eventListener);
        }

        return result;
    }

    public void run() {
        while (true) {
            synchronized (events) {
                try {

                    events.wait();

                } catch (InterruptedException e) {
                }
                while (!events.isEmpty()) {
                    notifyTrackingEvent(events.remove(0));
                }
            }
        }
    }
}
